I have a following piece of code:-
$returnArr = $this->master_model->fetch_all_data($data, $selectString,$limit, $offset);
foreach($returnArr as $row)
{
if (array_key_exists($data.'_image', $row))
{
$img = base_url()."uploads/$data/". $row[$data.'_image'];
$row[$data.'_image'] = $img;
}
}
print_r($returnArr);
The $return is in the following format:
Array ( [0] =>
Array ( [sticker_image] => post_1462515402.jpg
[sticker_code] => :* )
[1] => Array ( [sticker_image] => post_1462515510.jpg
[sticker_code] => ^=^ )
[2] => Array ( [sticker_image] => post_1462515532.jpg
[sticker_code] => >_<* )
[3] => Array ( [sticker_image] => post_1462515539.jpg
[sticker_code] => :(( ) )
Now, in the following line of code, I am changing [sticker_image] to a link:
if (array_key_exists($data.'_image', $row))
{
$img = base_url()."uploads/$data/". $row[$data.'_image'];
$row[$data.'_image'] = $img;
}
Still, the changes doesn't take place. It's still coming as
[sticker_image] => post_1462515402.jpg
What am I doing wrong?
$row[$data.'_image'] = $img; will change only local copy of array element.
To change actual array element you must loop with reference:
$returnArr = ['a' => 'b'];
foreach ($returnArr as &$row) {
$row = 'cc';
}
var_dump($returnArr); // ['a' => 'cc'];
Related
I have a product variation combination ID. Hyphen (-) The characters between the strings represent the variation options id.
I want to make copies of other IDs for free variation options based on the main combination ID.
My codes:
function find_replace($array, $find, $replace){
$array = array_replace($array,
array_fill_keys(
array_keys($array, $find),
$replace
)
);
return $array;
}
function get_var_key($array, $value){
$key_name=false;
foreach ($array as $n=>$c)
if (in_array($value, $c)) {
$key_name=$n;
break;
}
return $key_name;
}
$get_free_keys = array(
"var1" => array(
"free1",
"free2"
),
"var2" => array(
"free3",
"free4"
)
);
$main_combine = "a1-b1-free1-c1-d1-free3";
$main_combine_explode = explode("-", $main_combine);
for($i=0; $i < count($main_combine_explode); $i++){
$get_key_by_value = get_var_key($get_free_keys,
$main_combine_explode[$i]); // return "var1" or "var2"
foreach($get_free_keys[$get_key_by_value] as $values){
$find_combine = find_replace($main_combine_explode,
$main_combine_explode[$i], $values);
$combines[] = implode("-", $find_combine);
}
}
print_r($combines);
Wrong result:
Array
(
[0] => a1-b1-free1-c1-d1-free3 // main combine (ok)
[1] => a1-b1-free2-c1-d1-free3 // ok
[2] => a1-b1-free1-c1-d1-free3 // wrong
[3] => a1-b1-free1-c1-d1-free4 // wrong
)
Result is incorrect
I want to get the following result:
Array
(
[0] => a1-b1-free1-c1-d1-free3-e1 // $main_combine
[1] => a1-b1-free1-c1-d1-free4-e1
[2] => a1-b1-free2-c1-d1-free3-e1
[3] => a1-b1-free2-c1-d1-free4-e1
)
or
Array
(
[var1] => Array
(
[0] => a1-b1-free1-c1-d1-free3 // $main_combine
[1] => a1-b1-free2-c1-d1-free3
)
[var2] => Array
(
[0] => a1-b1-free1-c1-d1-free4
[1] => a1-b1-free2-c1-d1-free4
)
)
Thank you.
You can use get_combinations and str-replace and do:
$template = "a1-b1-#FIRST#-c1-d1-#SECOND#-e1";
foreach (get_combinations($get_free_keys) as $e) {
$res[] = str_replace(['#FIRST#', '#SECOND#'], $e, $template);
}
Live example: 3v4l
I have some array like this
Array(
[0] => {"puroriid":"3902598","purorid":"3901727","iid":"3927478"}
[1] => {"puroriid":"3902599","purorid":"3901727","iid":"3927486"}
[2] => {"puroriid":"3902600","purorid":"3901727","iid":"3927486"}
)
Here i want to add some data to at the end of each looping so data will look as follow.
Array(
[0] => {"puroriid":"3902598","purorid":"3901727","iid":"3927478","variation_name"=>"tiles" ,"hsn" =>"42424"}
[1] => {"puroriid":"3902599","purorid":"3901727","iid":"3927486","variation_name"=>"wood","hsn" =>"63636"}
[2] => {"puroriid":"3902600","purorid":"3901727","iid":"3927486","variation_name"=>"granite","hsn" =>"66656"}
)
I tried array push method but its created another index , instead of adding new data to last
below is my code.
$items = array();
$variations = array();
if ($purchaseOrderDetails->getOrderItems())
{
foreach ($purchaseOrderDetails->getOrderItems() as $key => $item)
{
$items[] = strval($item);
$variations[] = strval(new InventorySetVariation($item->getIsvid()));
}
}
Utility::ajaxResponseTrue("", array("po" => strval($purchaseOrderDetails), "items" => $items, "variations" => $variations));
Here I want to merge item and variations as one array .
how can I achieve it ?
Below is the example may help you to achieve similar goals of yours
<?php
$items = array(
0 => '{"puroriid":"3902598","purorid":"3901727","iid":"3927478"}',
1 => '{"puroriid":"3902599","purorid":"3901727","iid":"3927486"}',
2 => '{"puroriid":"3902600","purorid":"3901727","iid":"3927486"}'
); //Your Items
$itmesNew = [];
foreach($items as $val)
{
$newItem = json_decode($val,true);
$newItem['variation_name'] = 'test'; //New Items Append
$newItem['hsn'] = '123'; //New Items Append
$itmesNew[] = json_encode($newItem); //Add as json
}
echo "<pre>";
print_r($itmesNew);
?>
Output
Array
(
[0] => {"puroriid":"3902598","purorid":"3901727","iid":"3927478","variation_name":"test","hsn":"123"}
[1] => {"puroriid":"3902599","purorid":"3901727","iid":"3927486","variation_name":"test","hsn":"123"}
[2] => {"puroriid":"3902600","purorid":"3901727","iid":"3927486","variation_name":"test","hsn":"123"}
)
I got an array of links which I am getting from source code. I am looping through the array with a foreach loop and adding the results into a new array.
The problem is: I don't want all the results in one array. But for each link a separate array after I looped over it.
The array I am looping through:
Array
(
[0] => Array
(
[0] => http://videos.volkswagen.nl/videos/videos/
)
[1] => Array
(
[0] => http://videos.volkswagen.nl/videos/service-videos/
)
)
The foreach:
$sourceCats = array();
foreach ($matchesAll as $links) {
$strSourceAll = implode("|",$links);
$source = file_get_contents("$strSourceAll");
htmlspecialchars($source);
$sourceCats[] = $source;
}
How the array sourceCats looks now:
Array
(
[0] => (source code from first link)
[1] => (source code from second link)
)
How I want it to look like:
Array
(
[0] => Array
(
[0] => (source code from first link)
)
[1] => Array
(
[0] => (source code from second link)
)
)
I have tried a few things but nothing worked. Is the idea clear?
Any help will be much appreciated.
<?php
$finalsourceCats = array();
$counter_sourceCats = 0;
$matchesAll = array(
0 => array(
0 => "http://videos.volkswagen.nl/videos/videos/"
),
1 => array(
0 => "http://videos.volkswagen.nl/videos/service-videos/"
)
);
foreach ($matchesAll as $links) {
$sourceCats = 'sourceCats';
$sourceCats = $sourceCats . "_" . $counter_sourceCats;
$sourceCats = array();
$strSourceAll = implode("|", $links);
$source = file_get_contents("$strSourceAll");
htmlspecialchars($source);
$sourceCats[] = $source;
$finalsourceCats[] = $sourceCats;
$counter_sourceCats += 1;
}
echo "<pre>"; print_r($finalsourceCats);
I have to arrays I would like to compare:
$original and $duplicate.
for example here is my original file:
print_r($original);
Array ( [0] => cat423 [1] => dog456 [2] => horse872 [3] => duck082 )
and here is my duplicate:
print_r($dublicate);
Array ( [0] => cat423 [1] => dug356 )
I compare them with array_diff:
$result = array_diff($original, $dublicate);
My result:
Array ( [1] => dog456 [2] => horse872 [3] => duck082 )
So far so good, but I need to make a difference between the values which are incorrect and the values which are completely missing. Is this possible?
A way would be to crawl the entire original array, afterwards you will have two arrays, missings and duplicates.
$original = array("cat423", "dog456", "horse872", "duck082");
$duplicate = array("cat423", "dug356");
$missings = $duplicates = array();
foreach ($original as $val) {
if (in_array($val, $duplicate))
$duplicates[] = $val;
else
$missings[] = $val;
}
If you need the keys as well, you would have to alter the foreach loop like so:
foreach ($original as $key=>$val) {
if (in_array($val, $duplicate))
$duplicates[] = array("key" => $key, "value" => $val);
else
$missings[] = array("key" => $key, "value" => $val);
}
use in_array function
$original = array("cat423", "dog456", "horse872", "duck082");
$duplicate = array("cat423", "dug356");
foreach ($original as $item) {
if(in_array($item, $duplicate))
{
$dup[] = $item;
}
else
{
$miss[] = $item;
}
}
print_r($miss); #Array ( [0] => dog456 [1] => horse872 [2] => duck082 )
print_r($dup); #Array ( [0] => cat423 )
Working Preview
I have the following $qtips_messages array,
Array
(
[0] => Array
(
[id] => 1
[tips_title] => email_tips
[tips_message] => ex:xxxxx#xyz.com
[tips_key] => key_email
)
[1] => Array
(
[id] => 2
[tips_title] => website_tips
[tips_message] => ex:http://www.yahoo.co.in
[tips_key] => key_website
)
[2] => Array
(
[id] => 3
[tips_title] => zipcode_tips
[tips_message] => ex:60612
[tips_key] => key_zipcode
)
[3] => Array
(
[id] => 4
[tips_title] => phone_tips
[tips_message] => ex:1234567890
[tips_key] => key_phone
)
)
For example, I want to get the tips message for the tip_title 'email_tips'
I have tried with following code,
foreach($qtips_messages as $qtipsArray){
foreach($qtipsArray as $qkey=>$qvalue){
if($qtipsArray['tips_title'] == 'email_tips'){
$emailtipsMessage = $qtipsArray['tips_message'];
}
}
}
When i ran the above code i did not get any value.
What is wrong with this code?
You only need one loop:
$message = null;
foreach ($array as $tips) {
if ($tips['tips_title'] == 'email_tips') {
$message = $tips['tips_message'];
break;
}
}
I'd probably go for something like this though:
$message = current(array_filter($array, function ($tip) { return $tip['tips_title'] == 'email_tips'; }));
echo $message['tips_message'];
$array = array();
foreach($result AS $k =>$val)
$array[$val['tips_key']] = $val['tips_message'];
return $array;
Now the array $array have all the values for q-tips based on their keys...
Hope this will helps you...
try this way.
$array = array();
foreach($qtips_messages AS $k =>$val):
if($val['tips_title']=='email_tips')
{
$array[$val['tips_key']] = $val['tips_message'];
print_r($array);
break;
}
endforeach;