Ichki ichki qatorni kalitini qaytarib olish

Men bu bilan aqldan ozaman. Shunday qilib, men bu qatorni oldim:

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
    )
)
)

va "license_solid" bilan boshlangan qator qiymatlarini va o'z ichiga olgan qator qiymatini aks ettirishni xohlayman. "License_solid" yozuvlariga ega bo'lish uchun quyidagilarni bajaraman:

$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; 
  }
}

va u ishlaydi, lekin "$" ni o'z ichiga olgan qatorni qanday echish kerakligini tushunmayapman Bu misolda variation_3 berilishi kerak

3
Faqat sizning qatoringizdan pastadir. So'ng loop ichida faqatgina slug qiymatiga kiring va uning qiymati license_solid bilan boshlanganligini tekshiring va agar xohlasangiz, siz xohlagan subArray ni tanlang.
qo'shib qo'ydi muallif Rizier123, manba

6 javoblar

Foreach döngünüzü quyidagi kabi qayta yozing: -

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
1
qo'shib qo'ydi
kodingizning oxirida Array ([0] => 7042)
qo'shib qo'ydi muallif Ravi Hirani, manba
$ data [0] yozing
qo'shib qo'ydi muallif Ravi Hirani, manba
@RaffaeleProvinciali: Yangilangan javobni tekshiring
qo'shib qo'ydi muallif Ravi Hirani, manba
$ data [0] ni qaerda saqlashim kerak?
qo'shib qo'ydi muallif Stockafisso, manba
Array ([0] => 7042) "7042" ni qanday ko'rsatish mumkin?
qo'shib qo'ydi muallif Stockafisso, manba
Bajarildi! U hozir ishlaydi!
qo'shib qo'ydi muallif Stockafisso, manba
Hech qanday xatolik yo'q
qo'shib qo'ydi muallif Stockafisso, manba
Bu menga: "Ogohlantirish: Noqonuniy mag'lubiyat ofset", agar $ qiymati bir qator emas
qo'shib qo'ydi muallif Stockafisso, manba

Foreach döngünüzü quyidagi kabi qayta yozing: -

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
1
qo'shib qo'ydi
kodingizning oxirida Array ([0] => 7042)
qo'shib qo'ydi muallif Ravi Hirani, manba
$ data [0] yozing
qo'shib qo'ydi muallif Ravi Hirani, manba
@RaffaeleProvinciali: Yangilangan javobni tekshiring
qo'shib qo'ydi muallif Ravi Hirani, manba
Hech qanday xatolik yo'q
qo'shib qo'ydi muallif Stockafisso, manba
$ data [0] ni qaerda saqlashim kerak?
qo'shib qo'ydi muallif Stockafisso, manba
Array ([0] => 7042) "7042" ni qanday ko'rsatish mumkin?
qo'shib qo'ydi muallif Stockafisso, manba
Bajarildi! U hozir ishlaydi!
qo'shib qo'ydi muallif Stockafisso, manba
Bu menga: "Ogohlantirish: Noqonuniy mag'lubiyat ofset", agar $ qiymati bir qator emas
qo'shib qo'ydi muallif Stockafisso, manba

An'anaviy foreach noma'lum tuzilishga ega bo'lgan katta massiv uchun qatorni iterator yaxshi.

quyidagi ikkita usulni ko'rib chiqing

<?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);

Chiqish:

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

        )

)
1
qo'shib qo'ydi

An'anaviy foreach noma'lum tuzilishga ega bo'lgan katta massiv uchun qatorni iterator yaxshi.

quyidagi ikkita usulni ko'rib chiqing

<?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);

Chiqish:

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

        )

)
1
qo'shib qo'ydi

$ key argumenti bilan butun qatorda foreachdan foydalaning:

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
}
0
qo'shib qo'ydi
foreach ($ vari = $ value => $ qiymati sifatida) {$ slugValue = $ value ['attributes'] ['attribute_pa_license']; agar (0 === strpos ($ slugValue, 'license_solid')) {echo ''; echo $ slugValue; echo ''; }}
qo'shib qo'ydi muallif Stockafisso, manba
Men buni amalga oshirdim, lekin bu menga faqat license_type ning slug'ini beradi
qo'shib qo'ydi muallif Stockafisso, manba

$ key argumenti bilan butun qatorda foreachdan foydalaning:

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
}
0
qo'shib qo'ydi
foreach ($ vari = $ value => $ qiymati sifatida) {$ slugValue = $ value ['attributes'] ['attribute_pa_license']; agar (0 === strpos ($ slugValue, 'license_solid')) {echo ''; echo $ slugValue; echo ''; }}
qo'shib qo'ydi muallif Stockafisso, manba
Men buni amalga oshirdim, lekin bu menga faqat license_type ning slug'ini beradi
qo'shib qo'ydi muallif Stockafisso, manba
PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin