$message_detected = "#A# #B#";
Array ( [0] => Array ( [0] => 923126812536 [1] => Mudassar [2] => Kasur )
[1] => Array ( [0] => 923006542399 [1] => Zubair [2] => Lahore ) );
There are 2 arrays
foreach ($array as $new){
$find = array('/#A#/', '/#B#/', '/#C#/', '/#D#/', '/#E#/', '/#F#/', '/#G#/', '/#H#/', '/#I#/', '/#J#/', '/#K#/', '/#L#/', '/#M#/', '/#N#/', '/#O#/', '/#P#/', '/#Q#/', '/#R#/', '/#S#/', '/#T#/', '/#U#/', '/#V#/', '/#W#/', '/#X#/', '/#Y#/', '/#Z#/');//26
$replace = array($new[1], $new[2], $new[3], $new[4], $new[5], $new[6], $new[7], $new[8], $new[9], $new[10], $new[11], $new[12], $new[13], $new[14], $new[15], $new[16], $new[17], $new[18], $new[19], $new[20], $new[21], $new[22], $new[23], $new[24], $new[25], $new[26]);
$message_detected = preg_replace($find, $replace, $message_detected);
//Message should be change each time despit of only first
echo "Messsage :". $message_detected."</br></br></br></br>";
}
Result Should be
Messsage :Mudassar Kasur
Messsage :Zubair Lahore
But it shows
Messsage :Mudassar Kasur
Messsage :Mudassar Kasur
Please Guide me where i am wrong . And how to get Required Result
You're overwriting your variable on each loop. It should be a different one:
$template = "#A# #B# #C#";
$values = array (
array ( 923126812536, 'Mudassar', 'Kasur'),
array ( 923006542399, 'Zubair', 'Lahore'),
);
$vars = array('#A#', '#B#','#C#','#D#');
foreach($values as $lst) {
$result = str_replace($vars, $lst, $template);
echo $result, "\n";
}
(I changed variable names for readability).
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 a simple Two array
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
When I print this arrays it should be like following:
Array
(
[0] => Array
(
[Peter] => 22
[Clark] => 32
[John] => 28
)
)
Array
(
[0] => Array
(
[demo] => 22
)
)
But I want to create third array which will be show demo kye value into first array like following:
Array
(
[0] => Array
(
[Peter] => 22
[Clark] => 32
[John] => 28
[demo] => 22
)
)
Can we do two array into single array in PHP like Above
Not sure what are you trying to achieve here...little more context would be helpful. But this is how you can do this,
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
$result[] = array_merge($ages[0],$ages1[0]);
This would do the job.
<?php
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
$output = prepend_array($ages,$ages1);
print_r($output);
// Function to prepend arrays
function prepend_array()
{
$num_args = count(func_get_args());
$new_array = array();
foreach (func_get_args() as $params){
foreach($params as $out_key => $param)
{
foreach($param as $key => $value)
$new_array[$out_key][$key] = $value;
}
}
return $new_array;
}
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 an 2d array which returns me this values:
Array (
[0] => Array (
[0] => wallet,pen
[1] => perfume,pen
)
[1] => Array (
[0] => perfume, charger
[1] => pen,book
).
Out of this i would like to know if it is possible to create a function which would combine the array going this way,and create a new one :
if for example [0] => Array ( [0] => wallet,pen [1] => perfume,pen ) then should be equal to
[0] => Array ( [0] => wallet,pen, perfume ) because there is a common word else do nothing.
And also after that retrieve each words as strings for further operations.
How can i make the values of such an array unique. Array ( [0] => Array ( [0] => wallet [1] => pen [2] => perfume [3] => pen) ) as there is pen twice i would like it to be deleted in this way ( [0] => Array ( [0] => wallet [1] => pen [2] => perfume) )
It's just a matter of mapping the array and combining the inner arrays:
$x = [['wallet,pen', 'perfume,pen'], ['perfume,charger', 'pen,book']];
$r = array_map(function($item) {
return array_unique(call_user_func_array('array_merge', array_map(function($subitem) {
return explode(',', $subitem);
}, $item)));
}, $x);
Demo
This first splits all the strings based on comma. They are then merged together with array_merge() and the duplicates are removed using array_unique().
See also: call_user_func_array(), array_map()
Try this :
$array = Array (Array ( "wallet,pen", "perfume,pen" ), Array ( "perfume, charger", "pen,book" ));
$res = array();
foreach($array as $key=>$val){
$temp = array();
foreach($val as $k=>$v){
foreach(explode(",",$v) as $vl){
$temp[] = $vl;
}
}
if(count(array_unique($temp)) < count($temp)){
$res[$key] = implode(",",array_unique($temp));
}
else{
$res[$key] = $val;
}
}
echo "<pre>";
print_r($res);
output :
Array
(
[0] => wallet,pen,perfume
[1] => Array
(
[0] => perfume, charger
[1] => pen,book
)
)
You can eliminate duplicate values while pushing them into your result array by assigning the tag as the key to the element -- PHP will not allow duplicate keys on the same level of an array, so any re-encountered tags will simply be overwritten.
You can use recursion or statically written loops for this task.
Code: (Demo)
$result = [];
foreach ($array as $row) {
foreach ($row as $tags) {
foreach (explode(',', $tags) as $tag) {
$result[$tag] = $tag;
}
}
}
var_export(array_values($result));
Code: (Demo)
$result = [];
array_walk_recursive(
$array,
function($v) use(&$result) {
foreach (explode(',', $v) as $tag) {
$result[$tag] = $tag;
}
}
);
var_export(array_values($result));
I'm trying to use a specific object type from a JSON feed, and am having a hard time specifying it. Using the code below I grab and print the specific array (max) I want,
$jsonurl = "LINK";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json,true);
$max_output = $json_output["max"];
echo '<pre>';
print_r($max_output);
echo '</pre>';
And from the Array below, all I want to work with is the [1] objects in each array. How can I specify and get just those values?
Array
(
[0] => Array
(
[0] => 1309924800000
[1] => 28877
)
[1] => Array
(
[0] => 1310011200000
[1] => 29807
)
[2] => Array
(
[0] => 1310097600000
[1] => 33345
)
[3] => Array
(
[0] => 1310184000000
[1] => 33345
)
[4] => Array
(
[0] => 1310270400000
[1] => 33345
)
[5] => Array
(
[0] => 1310356800000
[1] => 40703
)
Well you could fetch those values with array_map:
$max_output = array_map(function($val) { return $val[1]; }, $json_output["max"]);
This requires PHP 5.3, if you use an earlier version, then you can use create_function to achieve similar results:
$max_output = array_map(create_function('$val', 'return $val[1];'), $json_output["max"]);
When you need to create new array which will contain only second values, you may use either foreach loop which will create it or use array_map() (just for fun with anonymous function available since php 5.3.0):
$newArray = array_map( function( $item){
return $item[1]
},$array);
Then you want to use last ("max" -> considering array with numeric keys) item, you can use end():
return end( $item);
And when you can process your data sequentially (eg. it's not part of some big getData() function) you can rather use foreach:
foreach( $items as $key => $val){
echo $val[1] . " is my number\n";
}
After you get $max_output...
for( $i = 0; $i < length( $max_output ); $i++ ) {
$max_output[$i] = $max_output[$i][1];
}
try this:
$ones = array();
foreach ($max_output as $r)
$ones[] = $r[1];