1

Below is my function:

public function getChildrenId(){
    $child_id =  array($this->db->query("SELECT customer_id
                              FROM " . DB_PREFIX . "customer
                              WHERE parent IN ( " .(int)$this->customer->getId().") "));
    foreach($child_id as $id =>$value) {
        $conv = json_decode(json_encode($value), true);
        $final = array_slice($conv,2);
        foreach ($final as $gchildren => $key) {
            sort($key);
            $gr = array_slice($key,0,$this->INF);
        }
    }
    return $gr;
}

It outputs:

array (size=3)
  0 => 
    array (size=1)
      'customer_id' => string '2' (length=1)
  1 => 
    array (size=1)
      'customer_id' => string '4' (length=1)
  2 => 
    array (size=1)
      'customer_id' => string '7' (length=1)

I am trying to get the values of the nested arrays. When I use foreach I only get data from array[0]. I also tried slicing the parent array and still didn't get it right, it outputs array,array,array.

I would like to extract these arrays values to a new array that I can use to query the database. final_array = array (2,4,7). Thank you in advance!

0

1 Answer 1

2

If your array looks like this, then the foreach should create the array your looking for.

array (size=3)
  0 => 
    array (size=1)
      'customer_id' => string '2' (length=1)
  1 => 
    array (size=1)
      'customer_id' => string '4' (length=1)
  2 => 
    array (size=1)
      'customer_id' => string '7' (length=1)

The following php will output array(2,4,7);

<?php

$aNewArray = array();

foreach($aArray as $aArray){

    $aNewArray[] = $aArray['customer_id'];

}

var_dump($aNewArray);

?>

You dont need a multidimensional array for this though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.