Jump to content

[SOLVED] Putting Dynamic Arrays into array_intersect


BrianCoyDesign

Recommended Posts

Hello fellow freaks... ;D

 

I'm hoping someone can help me.  I've created some code that takes an unknown number or arrays and creates a string (ugh) that can be placed into the array_intersect variable for comparison.

 

$c=0;

$newArray='';

foreach ($ship_methods_allowed as $module) {

$newArray .= '$ship_methods_allowed['.$c.'] , ';

$c++;

}

$newArray = rtrim($newArray,' , ');

 

eval("\$newArray = $newArray;");

$result = array_intersect($newArray);

 

 

If I put in more than one array I get the error that an unexpected , (comma) has been seen in the code.  I need the comma there and I need each variable to be seen as an array to get the results.

 

The $newArray variable comes out as a string "$ship_methods_allowed[0],$ship_methods_allowed[1]"

 

How do I get the array_intersect to see there correctly?

 

Any help would be appriciated.  I've been banging my head against a wall for a week now.

Link to comment
Share on other sites

$ship_methods_allowed contains an array of modules based on the id of the product. 

 

For example the first array $ship_methods_allowed[0]= array("UPS","Shipping Based on Total Order","In Store Pickup");

Second Array would contain $ship_methods_allowed[1]= array("UPS","Shipping Based on Total Order","Freight");

Third - $ship_methods_allowed[2]= array("UPS","Shipping Based on Total Order");

 

So by comparing these I want to get back $result = array("UPS","Shipping Based on Total Order");

 

I can then use the result to turn on those 2 modules so they show on the shipping page.

 

var_dump($ship_methods_allowed) returns

 

array(1) { [0]=> array(4) { [0]=> string(15) "In Store Pickup" [1]=> string(0) "" [2]=> string(3) "UPS" [3]=> string(0) "" } }

 

 

Link to comment
Share on other sites

1) I don't have a php environment handy, so this is completely untested.

 

2) Assuming the number of element in $ship_methods_allowed is unpredictable, I would try something like this:

 

$eval_string = "$result = array_intersect(";

for( $i = 0; $i < count( $ship_methods_allowed ); $i++ )
{
$eval_string .= "$ship_methods_allowed[" . $i . "], ";	
}

//trim off last ", "
$eval_string = substr($eval_string, 0, -2);

$eval_string .= ");";

eval($eval_string);

//now you should have a $result variable containing the intersection

 

3) Your var_dump() contradicted your explanation of what $shipping_methods_allowed contains. You should see everything in var_dump() that you expect to be in the array.

Link to comment
Share on other sites

1) I don't have a php environment handy, so this is completely untested.

 

2) Assuming the number of element in $ship_methods_allowed is unpredictable, I would try something like this:

 

$eval_string = "$result = array_intersect(";

for( $i = 0; $i < count( $ship_methods_allowed ); $i++ )
{
$eval_string .= "$ship_methods_allowed[" . $i . "], ";	
}

//trim off last ", "
$eval_string = substr($eval_string, 0, -2);

$eval_string .= ");";

eval($eval_string);

//now you should have a $result variable containing the intersection

 

3) Your var_dump() contradicted your explanation of what $shipping_methods_allowed contains. You should see everything in var_dump() that you expect to be in the array.

 

I'm getting Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING  using the above code.

 

My statement was off as I only had one array.

Link to comment
Share on other sites

When I modified the code slightly

 

$eval_string = "$result = array_intersect(";

for( $c = 0; $c < count( $ship_methods_allowed )+1; $c++ )
{
	$eval_string .= "$ship_methods_allowed[$c], ";	
}

//trim off last ", "
$eval_string = substr($eval_string, 0, -4);

$eval_string .= ");";

//eval($eval_string);
eval("\$eval_string=\"$eval_string\";");

 

I get = array_intersect(Array, Array);

as the result.

 

If I leave the eval the way it was in your code same I get an error on the equal sign.  I'm not sure why $result doesn't flow through to make the result show-up.

 

suggestions?

Link to comment
Share on other sites

i get no errors in PHP 5 can you paste the specific error you are getting ???

 

Parse error: syntax error, unexpected '=' in /home/myprinti/public_html/components/com_virtuemart/themes/default/templates/checkout/list_shipping_methods.tpl.php(96) : eval()'d code on line 1

 

PHP Version 5.2.9

Link to comment
Share on other sites

please paste the full code so we can determine where the error is.

 

i have tested your above code in PHP 4 and 5 and i get no errors

 

for($i=0;$i<count($searchShippersFinal);$i++){
             $db = new ps_DB;
$q = "SELECT DISTINCT custom_shipping_carrier FROM jos_vm_category WHERE category_id='". $searchShippersFinal[$i] ."'";
$db->query($q);
$db->next_record();

$ship_methods_allowed[$i] = explode( "|", $db->f('custom_shipping_carrier'));
for($j=0;$j<count($ship_methods_allowed[$i]);$j++){
	if($ship_methods_allowed[$i][$j]!=""){
		//echo $ship_methods_allowed[$i][$j] . "<br>";
	}
}
echo "<br>";
}
//Now that all the array have been created I need to take an put them all into one statement and compare ALL the ARRAYS at the same time to come up with final list.
$ship_methods_allowed[1] = array("UPS","Shipping Based on Total Order","In Store Pickup");
//$ship_methods_allowed[2] = array("UPS","Shipping Based on Total Order","In Store Pickup");
//$result = array_intersect($newArray,$ship_methods_allowed[1],$ship_methods_allowed[2]);


$eval_string = "$result = array_intersect(";

for( $c = 0; $c < count( $ship_methods_allowed )+1; $c++ )
{
	$eval_string .= "$ship_methods_allowed[$c], ";	
}

//trim off last ", "
$eval_string = substr($eval_string, 0, -4);

$eval_string .= ");";

eval($eval_string);
//eval("\$eval_string=\"$eval_string\";");
//$result = $eval_string;
echo "HERE:" . $result . "-" . $eval_string . "<br>";

Link to comment
Share on other sites

if you uncomment my code and comment his out it works :P

 

Are you refering to these two lines?

 

eval($eval_string);

//eval("\$eval_string=\"$eval_string\";");

 

When I switch those I get no error but I also don't get an clean answer

 

I receive this:  HERE:- = array_intersect(Array, Array);

 

For the line

 

echo "HERE:" . $result . "-" . $eval_string . "<br>";

 

 

I get nothing for print_r ($result);

 

I need an array of the results from the array_intersect.

Link to comment
Share on other sites

Solved.... Ever have one of those walk away and then it will make sense moments?

 

The problem was I was using " double quotes in the creation instead of single quotes.  Here's the solution...

 

$eval_string = '$result = array_intersect(';

for($c=0; $c < count( $ship_methods_allowed ); $c++)
{
	$eval_string .= '$ship_methods_allowed[' . $c . '], ';	
}

//trim off last ", "
$eval_string = substr($eval_string, 0, -2);

$eval_string .= ');';

eval($eval_string);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.