3

I'm going crazy with this. So, let's say I got this array:

Array
(

  [0] => Array
    (
     [variation_name] => variation_1
     [license_type] => Array
    (
      [slug] => license_x
      [price] => price_x
      [dimensions] => dimensions_x
    )
 )

  [1] => Array
   (
    [variation_name] => variation_2
    [license_type] => Array
    (
      [slug] => license_y
      [price] => price_y
      [dimensions] => dimensions_y
    )
)

  [2] => Array
   (
  [variation_name] => variation_3
  [license_type] => Array
    (
      [slug] => license_solid_z
      [price] => price_z
      [dimensions] => dimensions_z
    )
)
)

and I want to echo the array values beginning with "license_solid" and the value of the array that contains it. To have the "license_solid" entries I run the following:

$attribute_pa_licenses = array_column($array, 'license_type');
$attribute_pa_license_slug = array_column($attribute_pa_licenses, 'slug');

foreach ($attribute_pa_license_slug as $value) { 
  if (0 === strpos($value, 'license_solid')) {

   echo $value; 
  }
}

and it DO works, but I'm not understanding how to echo also the array "containing" $value in this example it should give variation_3

1
  • 2
    Just loop through your array. Then inside the loop simply access the slug value and check if its value starts with license_solid and if yes you got the subArray you want. Commented Feb 27, 2017 at 10:22

3 Answers 3

1

Rewrite your foreach loop as below:-

foreach ($attribute_pa_license_slug as $value) { 
  if(!empty($value['license_type']['slug'])){
  $slug = $value['license_type']['slug'];
   if (strpos($slug, 'license_solid') !== false) {   
     echo $slug; // echo your matched value
     $data[] = $value; // store your array in data array
   }
 }
}
print_r($data); // print arrays who has valid slug values
Sign up to request clarification or add additional context in comments.

5 Comments

It give me: "Warning: Illegal string offset " as if $value it's not an array
DONE! IT WORKS NOW!
It coorectly prints: Array ( [0] => 7042 ) How could I show just "7042"?
Where should I put $data[0] ?
at the end of your code where you got Array ( [0] => 7042 )
1

Traditional foreach is good for array which structure is known but for large array with unknown structure array iterator is good.

have a look on below two methods

<?php
$array = Array
(

    '0' => Array
    (
        'variation_name' => 'variation_1',
        'license_type' => Array
        (
            'slug' => 'license_x',
            'price' => 'price_x',
            'dimensions' => 'dimensions_x'
        )
    ),

    '1' => Array
    (
        'variation_name' => 'variation_2',
        'license_type' => Array
        (
            'slug' => 'license_y',
            'price' => 'price_y',
            'dimensions' => 'dimensions_y'
        )
    ),

    '2' => Array
    (
        'variation_name' => 'variation_3',
        'license_type' => Array
        (
            'slug' => 'license_solid_z',
            'price' => 'price_z',
            'dimensions' => 'dimensions_z'
        )
    )

);

//METHOD 1 - For known structured array
foreach($array AS $key => $val) {
    $slug = $val['license_type']['slug'];
    if (strpos($slug, 'license_solid') !== false) {
        $data[] = $array[$key];
    }
}

print_r($data);



//METHOD 2 - For unknown structured array (use iterator for unknow and large structured)
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));


$data = array();
foreach ($it as $key => $val) {

    $ar = $it->getSubIterator($it->getDepth() - 1);
    if($key == 'slug' && strpos($val, 'license_solid') !== false){
        $data[] = (array) $ar;
    }
}

print_r($data);

Output:

Array
(
    [0] => Array
        (
            [variation_name] => variation_3
            [license_type] => Array
                (
                    [slug] => license_solid_z
                    [price] => price_z
                    [dimensions] => dimensions_z
                )

        )

)

Comments

0

Use foreach over whole array with $key argument:

foreach ($array as $key => $value) {
    $slugValue = $value['license_type']['slug'];

    if (...) { echo $slugValue; }

    // $key contains current array index (0..2 for array in example)
    // $value contains array with variation_name, license_type
}

2 Comments

I done it but it still gives me just the Slug of the license_type
foreach ($variations as $key => $value) { $slugValue = $value['attributes']['attribute_pa_license']; if (0 === strpos($slugValue, 'license_solid')) { echo '<span class="ss_product_resolution_title">'; echo $slugValue; echo '</span>'; }}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.