hello,

how do I go about moving part of an associative array into another array?
$newArray = array();

looking at the sample array i copy/pasted below, how do move array [1] into a new array ($newArray)???

sample array:


Array
(
    [0] => Array
        (
            [node_inx] => 1
            [node_name] => business
            [node_parent] => 0
            [up1_inx] => 
            [up1_name] => 
            [up1_parent] => 
            [up2_inx] => 
            [up2_name] => 
            [up2_parent] => 
            [up3_inx] => 
            [up3_name] => 
            [up3_parent] => 
        )

[1] => Array
    (
        [node_inx] => 2
        [node_name] => pets
        [node_parent] => 0
        [up1_inx] => 
        [up1_name] => 
        [up1_parent] => 
        [up2_inx] => 
        [up2_name] => 
        [up2_parent] => 
        [up3_inx] => 
        [up3_name] => 
        [up3_parent] => 
    )

[2] => Array
    (
        [node_inx] => 3
        [node_name] => technology
        [node_parent] => 0
        [up1_inx] => 
        [up1_name] => 
        [up1_parent] => 
        [up2_inx] => 
        [up2_name] => 
        [up2_parent] => 
        [up3_inx] => 
        [up3_name] => 
        [up3_parent] => 
    )

    Why do you want to "move" it when you could use it directly?

      Also, you're asking about "moving" part of an associative array, but your example has you wanting to "move" an element of a numerically-indexed array.

        its a breadcrumb example i pulled of the net and i figured it would be easier if i separated the array.

        if i was to work from this array, whats the best way to create a breadcrump navigation from the array i have.

        i'm using the following code to iterate through the array to display the breadcrumb but i have a feeling im doing it the long way.. is there a better way to do this?

        foreach ($testArray as $key1 => $value1)
        { if($value1['node_inx'] == $catID)
        { if(!empty($value1['up3_inx']))
        { echo '<li><a href="dbtest2.php?id='. $value1['up3_inx'] . '&par='. $value1['up3_parent'] . '&cat=' . $value1['up3_name'] . '">' . $value1['up3_name'] . '</a></li>'; }
        if(!empty($value1['up2_inx']))
        { echo '<li><a href="dbtest2.php?id='. $value1['up2_inx'] . '&par='. $value1['up2_parent'] . '&cat=' . $value1['up2_name'] . '">' . $value1['up2_name'] . '</a></li>'; }
        if(!empty($value1['up1_inx']))
        { echo '<li><a href="dbtest2.php?id='. $value1['up1_inx'] . '&par=' . $value1['up1_parent'] . '&cat=' . $value1['up1_name'] . '">' . $value1['up1_name'] . '</a></li>'; }
        if(!empty($value1['node_inx']))
        { echo '<li><strong><a href="dbtest2.php?id='. $value1['node_inx'] . '&par=' . $value1['node_parent'] . '&cat=' . $value1['node_name'] . '">' . $value1['node_name'] . '</a></strong></li>'; }
        }
        }//end of outer foreach loop

          As it stands, your code is difficult to read. You need to indent it properly, e.g.,

          foreach ($testArray as $key1 => $value1)
          {
              if ($value1['node_inx'] == $catID)
              {
                  if (!empty($value1['up3_inx']))
                  {
                      echo '<li><a href="dbtest2.php?id='. $value1['up3_inx'] . '&par='. $value1['up3_parent'] .
                          '&cat=' . $value1['up3_name'] . '">' . $value1['up3_name'] . '</a></li>';
                  }
                  if (!empty($value1['up2_inx']))
                  {
                      echo '<li><a href="dbtest2.php?id='. $value1['up2_inx'] . '&par='. $value1['up2_parent'] .
                          '&cat=' . $value1['up2_name'] . '">' . $value1['up2_name'] . '</a></li>';
                  }
                  if (!empty($value1['up1_inx']))
                  {
                      echo '<li><a href="dbtest2.php?id='. $value1['up1_inx'] . '&par=' . $value1['up1_parent'] .
                          '&cat=' . $value1['up1_name'] . '">' . $value1['up1_name'] . '</a></li>';
                  }
                  if (!empty($value1['node_inx']))
                  {
                      echo '<li><strong><a href="dbtest2.php?id='. $value1['node_inx'] . '&par=' . $value1['node_parent'] .
                          '&cat=' . $value1['node_name'] . '">' . $value1['node_name'] . '</a></strong></li>';
                  }
              }
          }

          Anyway, it seems to me that you are trying to list the breadcrumbs for each page (category?), then have the loop figure out which page it is and then echo out the breadcrumbs as a list in HTML. However, you assume up to three levels of breadcrumbs, and then you leave them blank.

          I'd say that there are more flexible and easier ways of doing this if you want to hard code large arrays up front. For example, you can have one large array to list out all the pages, i.e., their ids, page names and URLs, e.g.,

          $page_directory = [
              '1' => ['name' => 'business', 'url' => '/whatever/business'],
              '2' => ['name' => 'pets', 'url' => '/whatever/business/pets'],
              '3' => ['name' => 'dogs', 'url' => '/whatever/business/pets/dogs']
          ];

          Then you have another large array to hard code the breadcrumbs by id, e.g.,

          $breadcrumbs = [
              '1' => [],
              '2' => ['1'],
              '3' => ['1', '2']
          ];

          This way, you just need to loop over $breadcrumbs[$catID]. On each iteration, access the $page_directory element corresponding to the value of the current element of $catID, and hence print the list element in HTML.

          Of course, this can get pretty unwieldy if you have many pages, but that's the nature of hard coding things.

            I might be tempted to put the data structure into a separate JSON file that could be loaded into an array when needed.

              Might be a good idea, though I suggest that you get the array structure right first. After that, figuring the representation in JSON will be trivial.

                Write a Reply...