Php Building array from other arrays

Php Building array from other arrays,php,arrays,for-loop,Php,Arrays,For Loop,I am trying to build a new array from simpleXmlElement. I am getting the information I want, just not in the right hierarchy. $xmlNew1 = new SimpleXMLElement($responseNew1); $test = array(); foreach ($xmlNew1->children() as $newChild){ $classIden[] = (string)$xmlNew1->class['id']; $item[] = (string)$xmlNew1->code; $family[] = (string)$xmlNew1->family; for($i=0, $count = count($classIden); $i < $count; $i++) { $test[$item[$i]][$family[$i]]= $classIden[$i];

I am trying to build a new array from simpleXmlElement. I am getting the information I want, just not in the right hierarchy.

$xmlNew1 = new SimpleXMLElement($responseNew1);
$test = array();
foreach ($xmlNew1->children() as $newChild){
    $classIden[] = (string)$xmlNew1->class['id'];
    $item[] = (string)$xmlNew1->code;
    $family[] = (string)$xmlNew1->family;
    for($i=0, $count = count($classIden); $i < $count; $i++) {
        $test[$item[$i]][$family[$i]]= $classIden[$i];
    }
}

print_r($test);

this gives me:

Array
(
[9522] => Array
    (
        [Mens Hats] => 44
    )

[9522-NC-NO SIZE] => Array
    (
        [Mens Hats] => 44
    )

[B287CSQU] => Array
    (
        [Boys] => 1
    )

but I want

Array
(
[9522] => Array
(
    [family] => Mens Hats
    [classId] => 44
)

Any suggestion? Thanks!


#1

This should probably do it (to replace the main loop contents):

$id = (string)$newChild->class['id'];
$code = (string)$newChild->code;
$family = (string)$newChild->family;

$items[$code] = array(
    'family' => $family,
    'classId' => $id,
);

Edit

Forgot to use $newChild instead of $xmlNew1.


#2

I don't know your hierarchy because I do not know much about the HTML. You've hidden the structure. However, you should probably build the array in the format you look for directly.

Let us try to make that less complicated. Let's say you have a variable that would contain all the children. So you can pull apart the iteration and loading these children. Loading:

 $xmlNew1 = new SimpleXMLElement($responseNew1);
 $newChildren = $xmlNew1->children();

I can not say you if $xmlNew1->children() is sufficient to get you all the xml elements you are looking for, but let's just assume so.

Next part is about the iteration. As written you should build the $test array while you iterate over the children - as you partly already did:

$test = array();
foreach ($newChildren as $newChild) {
    ...
}

Missing part now is to create your structure in $test:

Array(
    [9522] => Array(
        [family] => Mens Hats
        [classId] => 44
    )

I like the suggestion @Jack gives here to first assign the values you want to extract to variables of their own and then create the entry.

$newItem = array(
    'family' => $family,
    'classId' => $id,
);
$test[$code] = $newItem;

Naturally you have to place that into the iteration. When the iteration is done, the $test array should have the format you're looking for.

I hope this is helpful.


#3

I don't need a foreach to pull every child call out?