Jump to content

Create array with other array elements


allinurl

Recommended Posts

Hello, I'm trying re create array 2 with the elements of array1 BUT just for the second column. and then print out the new array3. So far I have done what is below; I would appreciate so much if you guys could give a hand on this. Thanks

 

$array1 = array(10, 200, 290);

$array2 = array( array("rose", 10, 795, "test"),
               array("daisy", 200, 795, ""),
               array("orchid", 290, 795, "test") 
             );

for ($row = 0; $row < 4; $row++)
{

    for ($col = 0; $col < 4; $col++)
    {
if($array2[$row][3]!=""){        	
	echo $array3[$row][1] = $x[$col];
}
    }
}

Link to comment
https://forums.phpfreaks.com/topic/96003-create-array-with-other-array-elements/
Share on other sites

basically what Im trying to do is that if $array2 has an empty value then create an array3 with the same values of array2 execpt the element that has the empty value.

 

so array3 will look lihe this:

 

$array3 = array( array("rose", 10, 795, "test"),
               array("orchid", 200, 795, "test") 
             );

 

Notice that the there is no more "array("daisy", 200, 795, "")" and the value of "orchid" now is 200 because that's the second value of array1 Thanks again

If I understand what you are trying to do, you should remove all of the rows that you don't want first, then remap the other column. I think you would do something like this:

//Remove all of the elements that have a blank value at offset 3
for ($i=0;$i<sizeof($array2);$i++))
{
if ($array2[$i][3] == "")
	$array2 = array_splice($array2, $i, 1);
}

//Remap the numbers
for ($i=0;$i<sizeof($array2);$i++))
$array2[$i][1] = $array1[$i];

yeah good aproach, but im not quite sure why when I print_r is giving me the element that was empty, some ideas? thanks

 

$array1 = array(10, 200, 290);

$array2 = array( array("rose", 10, 795, "test"),
               array("daisy", 200, 795, "test"),
               array("orchid", 290, 795, "")
             );

//echo sizeof($array2);
//Remove all of the elements that have a blank value at offset 3
for ($i=0; $i < sizeof($array2); $i++){
        if ($array2[$i][3] == ""){
                $array2 = array_splice($array2, $i, 1);
}
}
print_r($array2);
//Remap the numbers
for ($i=0; $i < sizeof($array2); $i++){
        $array2[$i][1] = $array1[$i];
print_r($array2);
}

ok so far I got the following working but now the problem is that if I need to bump down more than one item in the list, since it resizes the array each time it will screw up. what should I do? Thanks

 

function bump_down(&$array, $index) {
     do {
         $array[$index][0] = $array[$index+1][0];
         $array[$index][2] = $array[$index+1][2];
         $array[$index][3] = $array[$index+1][3];
         
     } while (isset($array[++$index + 1]));
     unset($array[$index]);
}

$array2 = array( array("rose", 10, 795, "test"),
               array("daisy", 200, 795, ""),
               array("orchid", 290, 795, "test")
             ); 
             

//Remove all of the elements that have a blank value at offset 3
for ($i=0; $i < sizeof($array2); $i++){
       if ($array2[$i][3] == ""){
                bump_down($array2, $i);
        }
} 

print_r($array2);  

Hi What about this

 

  $array2 = array( array("rose", 10, 795, "test"),
               array("daisy", 200, 795, ""),
               array("orchid", 290, 795, "test") 
             );
$array3 = $array2;

$count = count($array3);
for($i = 0 ; $i < $count ; $i++){
	if(array_search('',$array3[$i]) !== false){
		unset($array3[$i]);
	}
}
echo '<pre>';
print_r($array2);
print_r($array3);
echo '</pre>';

 

Hi What about this

 

Thanks  tapos, thats very helpful to getrid of the empty ones, if you see above, Im trying to rearrange  the values of the second column.

 

"Notice that the there is no more "array("daisy", 200, 795, "")" and the value of "orchid" now is 200 because that's the second value of array1 Thanks again"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.