I would like to do that :
$product[] = Array (
"article_title" => valeures,
"article_id" => ,
"article_value" => ,
"article_price" =>
"article_picture1" => ,
"article_picture2" => ,
"article_picture3" => ,
"article_picture4" => ,
"article_friends[]" => array ( "name" => ,
),
);
But I know it won't work, I've been looking for hours on the net and on php.net, but I don't know how to do.
What I want to do, is getting a product[] array that can handle product[1]; product [2] ... and inside it, I got the same process : the "article_firends[]" will be autoincremented to do something like that :
In product[1] : artcile_friends[1][name] = John ; article_freidns[2][name] = Nina ... and so on with product[2] ...
This has been implementedin a foreach loop so it Should manage the keys automatically ..
How can I build it ?
Many Thanks,
Miles
"article_friends[]" => array ( "name" => ,
doesn't create a new article_friends sub array. It creates a key in the parent $product array whose name happens to be article_friends[].
Remove the quotes from around the key, so you end up with
article_friends[] => array ( "name" => , ...
This totally does work.
You're getting an error because of valeures, either this is a variable, than add a '$' or it's a string, than add quotes.
"article_friends[]" => array ( "name" => , is wrong too, you have to add a value after "name".
The trailing comma in the 'article_friends[]' line should be omitted to.
Related
I have the following array (print_r output) -
Array ( [Ds_Date] => 08%2F06%2F2018 [Ds_Hour] => 12%3A46 [Ds_SecurePayment] => 1 [Ds_Amount] => 1000 [Ds_Currency] => 978 [Ds_Order] => 180608104552 ........
How do I set the Ds_Amount key and value to a variable to use later?
thanks
Craig.
I think you should create your array this way:
<?php
$array = array(
"Ds_Date" => "08%2F06%2F2018",
"Ds_Hour" => " 12%3A46",
"Ds_SecurePayment" => "1",
...
);
?>
And then acces it like this:
$array["Ds_Date"]
Hope it helps...
I have a Master PHP array at the bottom of my question. It all works as it should and encodes to json perfectly. My problem is confined to the "content" object shown in the array.
The 'content' object should contain up to ten sub-arrays ($newSubArray0-9)each sub-array being populated with image data according to this simple loop below (the data is all available from a prior database query - so no issues there).
//get data for the 'content' object sub arrays....
for( $i = 0; $i<9; $i++ ) {
$newSubArray.$i =
[
'url' => $uploadspath.$media_url.$i,//main image
"type" =>$type.$i,//media type
"caption"=>$caption.$i
];
}
Now...in the master array below I simply need to be able to exclude or delete any of the $newSubArray (0-9) instances IF its corresponding media-condition yields an empty media result ie (!media_url.$i) or ($media_url.$i="").
I have tried...
If (media-conditon)
...{$newSubArray.$i=UNSET($newSubArray.$i);}
...{$newSubArray.$i=array_filter($newSubArray.$i);}
...{$newSubArray.$i=array_values($newSubArray.$i);}
All of these attempts affect the subarray elements I am trying to delete entirely (based on the condition) BUT leave a trace of the subarray itself eg 'none'. That trace causes the subsequent data validation to fail. The vailidation is being done by a third party API to which the json data is sent--so I cannot change the validation. I need there to be absolutely NO record of any subarray for which there is no associated $media_url value.
Here is a portion of the Master Array with the sub array elements that I need to conditionally include or exclude depending on the availability of associated images. It all works fine ...just need to solve the problem above.
$data = json_encode
(
array
(
"location" =>array
(
"property_number_or_name" => $house_name_number,
"street_name" => $propaddress1,
"locality" => $propaddress2,
"town_or_city" => $town,
"postal_code" => $postcode,
"country_code" => "GB",
"coordinates"=>array
(
"latitude"=>(float)$latitude,
"longitude"=>(float)$longitude
)
),
"pricing" =>array
(
"rent_frequency" => $rent_frequency,
"currency_code" => "GBP",
"price" => (int)$price,
"transaction_type" => "rent"
),
"detailed_description" =>array
(
array
(
"text" => $description
)
),
"content" => array
(
$newSubArray0,//remove this sub array completely if no media_url0
$newSubArray1,//remove this sub array completely if no media_url1
$newSubArray2,//remove this sub array completely if no media_url2
$newSubArray3,//remove this sub array completely if no media_url3
$newSubArray4,//remove this sub array completely if no media_url4
$newSubArray5,//remove this sub array completely if no media_url5
$newSubArray6,//remove this sub array completely if no media_url6
$newSubArray7,//remove this sub array completely if no media_url7
$newSubArray8,//remove this sub array completely if no media_url8
$newSubArray9 //remove this sub array completely if no media_url9
),
"google_street_view"=>array
(
"heading"=>(float)$pov_heading,
"pitch"=> (float)$pov_pitch,
"coordinates"=> array
(
"latitude"=>(float)$pov_latitude,
"longitude"=>(float)$pov_longitude
)
),
"display_address"=> $display_address,
"summary_description"=> $summary
,JSON_UNESCAPED_SLASHES);
You likely want unset but to not store the result of the call in the same place.
Yours:
If (media-conditon)
...{$newSubArray.$i=UNSET($newSubArray.$i);}
Update:
If (media-conditon)
...{UNSET($newSubArray.$i);}
You could also do this more efficiently by validating your media condition before you set the element in the content data.
I would like to add an array to within an existing array.
I am tryin to use array_push which works as long as i dont try to assign a key to the array (if i try to add a key i get a syntax error... :-()
This is my initial array:
$ResultArray = array(
"TransactionDate" => "$TransactionDate",
"tx"=>array(
"0"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" => "$PaymentConfirmedCount"
),
"1"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" => "$PaymentConfirmedCount"
)
)
);
i would then like to add:
$ArrayTOAdd = array(
"0"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" =>
"$PaymentConfirmedCount"
)
);
if I try:
array_push($ResultArray->tx, $ArrayTOAdd);
BUT this does not work and results in a warning of "array_push() [function.array-push]: First argument should be an array"
if i try this :
array_push($ResultArray, $ArrayTOAdd);
it just adds the array but not to $ResultArray->tx
Any suggestions would be greatly welcomed!
You have to access the element in the array with $ResultArray["tx"] and not $ResultArray->tx. The second one is for the access to members in a php class. So an
array_push($ResultArray["tx"], $ArrayTOAdd);
should work.
I'm currently reading through some code in a drupal module and have come across the following in an associative array.
$this->replacements = array(
'%field' => $this->instance['label'],
'%bundle' => $bundles[$this->instance['entity_type']][$this->instance['bundle']]['label'],
);
What does the % in the key mean or is it just a string label
I think its just sting label.
$a = array("%name" => "pugazh", "vaalue" => "value");
print_r($a);
If you try like this in corephp it will return an output like this
Array ( [%name] => pugazh [vaalue] => value )
But i don't know what its mean in drupal.
$string = "php, photoshop, css";
I'm producing an array from the comma separated values above using the str_getcsv() function:
$array = str_getcsv($string);
Result:
Array ( [0] => php [1] => photoshop [2] => css )
How can I replace the key integers with a string tag for all elements like seen below?
Array ( [tag] => php [tag] => photoshop [tag] => css )
Edit: if not possible what alternative can I apply? I need the array keys to be identical for a dynamic query with multiple OR clauses
e.g.
SELECT * FROM ('posts') WHERE 'tag' LIKE '%php% OR 'tag' LIKE '%photoshop% OR 'tag' LIKE '%css%'
I'm producing the query via a function that uses the array key as a column name and value as criteria.
That is not possible. You can have only one item per key. But in your example, the string "tag" would be the key of every item.
The other way arround would work. So having an array like this:
array('php' => 'tag', 'photoshop' => 'tag', 'css' => 'tag');
This might help you, if you want to save the "type" of each entry in an array. But as all the entries of your array seems to be from the same type, just forget about the "tag" and only store the values in a numeric array.
Or you can use a multidimensional array within the numeric array to save the type:
array(
0 => array( 'type' => 'tag', 'value' => 'php' ),
1 => array( 'type' => 'tag', 'value' => 'photoshop' ),
2 => array( 'type' => 'tag', 'value' => 'css' )
);
But still using just an numeric array should be fine if all the entries have the same type. I can even think of a last one:
array(
'tag' => array('php', 'photoshop', 'css')
);
But even if I repeat myself: Just use an ordinary array and name it something like $tag!
BTW: explode(', ', %string) is the more common function to split a string.
To build SQL statement you might do something like this:
// ... inside you build function
if(is_array($value)){
$sql .= "'".$key."' LIKE '%."implode("%' OR '".$key."' LIKE '%", $value)."%'";
} else {
$sql .= "'".$key."' LIKE '%".$value."%'";
}
This might look confusing but it's much cleaner than runnig into two foreach-loops building the query.
That won't work. Your array keys have to be unique, or subsequent additions will simply overwrite the previous key.
As the others said, keys have to be unique. Otherwise, which element should be returned if you access $arr['tag']? If you now say "all of them", then create a nested array:
$array = array();
$array['tag'] = str_getcsv($string);
The value $array['tag'] will be another array (the one you already have) with numerical keys. This makes, because you have a list of tags and lists can be represented as arrays too.
Understanding arrays is very important if you want to work with PHP, so I suggest to read the array manual.
Assuming you know the size of your array beforehand
$tags = array("tag1","tag2","tag3");
$data = array("php","photoshop","css");
$myarray = array();
for ($i=0; $i<count($data); $i++) {
$myarray[$i] = array($data[$i], $tags[$i]);
}
Then
echo $myarray[0][0] . ", " . $myarray[0][1];
Outputs:
php, tag1