PHP: How to implode these array values? - php

I have these code:
$arr = array($check);
When I try to
echo "<pre>";
die(print_r($arr));
it gives me result in the Firebug
<pre>Array
(
[0] => Array
(
[0] => 2
[1] => 1
)
)
Now, I want an output that will give me '2,1'.
How could I do that in PHP? Do I need to use implode?

Yes you need to implode:
echo implode(',', $check);
You don't need to put it inside an array:
$arr = array($check); // no need

Related

Create Array newline and add values into multidimensional Array

I have a set of values as a string, like so:
MyCustomProductID1
MyCustomProductID2
Then I proceed to create an array out of these values with explode( "\n", $myProductIdString)
Now I have additional data (strings) that I want to combine with the value from my first array. I want that simple array into a multidimensional one:
Array
(
[0] => Array
(
[id] => MyCustomProductID1
[url] => http://example.com/MyCustomProductID1.jpg
)
[1] => Array
(
[id] => MyCustomProductID2
[url] => http://example.com/MyCustomProductID2.jpg
)
)
How do I get that first array into a multidimensional one and push data along with it?
Instead of direct assigning values to array use loop-
<?php
$str = "MyCustomProductID1
MyCustomProductID2";
$arr = explode("\n", $str);
$result = [];
$url_array = ["http://example.com/MyCustomProductID1.jpg", "http://example.com/MyCustomProductID2.jpg"];
for($i=0;$i<count($arr);$i++){
$result[$i]['id'] = $arr[$i];
$result[$i]['url'] = $url_array[$i];
}
print_r($result);
?>

how to store two variables strings in 2d array in php

I'm working with multi-dimensional arrays in PHP. I want to store two string values in a two dimensional array. I have tried the following code:
$arr[][]=['string1']['string2'];
I have also tried:
$arr[][]="string1","string2";
Due to the comma I have a syntax error.
How can I fix this?
$arr = [];
$arr[] = ['string1'];
$arr[] = ['string2'];
// or simply
// $arr = [['string1'], ['string2']];
print_r($arr);
// output:
Array ( [0] => Array ( [0] => string1 ) [1] => Array ( [0] => string2 ) )
If you want 'string1' and 'string2' to be the key and subkey in an array then you use:
$arr = [];
$arr['string1']['string2'] = 'somevalue';

pull dynamic element in single array

I am trying to pull dynamic element in single array but result is not showing properly
Ex:
$array =array();
$element="'abc1','abc2'";
$array=array('abc',$element);
//I want result like that:
array[
[0]=>abc,
[1]=>abc1,
[2]=>ab
]
If you need to parse a string of elements to an array you can use one of the csv functions. You may then merge your arrays.
$array = array('abc');
$string_elements = "'abc1','abc2'";
$array_elements = str_getcsv($string_elements, ',', "'");
$array = array_merge($array, $array_elements);
var_export($array);
Output:
array (
0 => 'abc',
1 => 'abc1',
2 => 'abc2',
)
Alternatively to add each array element to the end of another array you can push them like so using a splat:
array_push($array, ...$array_elements);
According to my understanding, $element is storing string not an array so even if you try to get output it will display in following format
Array
(
[0] => abc
[1] => 'abc1','abc2'
)
Instead of this you can store array in $element variable and use array_merge() function to get required output e.g.
$element = array('abc1','abc2');
$array = array('abc');
$result = array_merge($array,$element);
// Output
Array
(
[0] => abc
[1] => abc1
[2] => abc2
)

How to implement the request and response in php

I have URL like this, how we recive the value
like "abc.org/result.php?rq=[1,2,3,4]&rq1=[5]" etc
Can any one help me to convert rq into array when recive the value in result.php
You can try using explode() & array_map()
$rq = $_GET['rq'];
$arr = array_map(function($v){ return trim($v, ' []');}, explode(',', $rq));
print '<pre>';
print_r($arr);
print '</pre>';
You can use json_decode to convert into array
$a = json_decode($_GET['rq']);
print_r($a);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
You can use get request also:
Abc.com?req[]=1&req[]=2
$_GET['req']
Return
(1,2)

How to use single quote separated values from an array in a string prepared by using implode?

I've following array called $user_id_arr
Array
(
[0] => 92ecd33db4ddcdc28e025cae80f00208
[1] => 9def02e6337b888d6dbe5617a172c18d
[2] => a6d22e4cc3f65778a60b359842bcec82
)
Now I want a string containing above array values but each array value should be enclosed in single quotes. For it I tried following code but not succeed.
$user_ids = implode(",", $user_id_arr);
I'm getting following array:
92ecd33db4ddcdc28e025cae80f00208,9def02e6337b888d6dbe5617a172c18d,a6d22e4cc3f65778a60b359842bcec82
Can anyone please help me in this regard please? I want the desired array in following format:
'92ecd33db4ddcdc28e025cae80f00208','9def02e6337b888d6dbe5617a172c18d','a6d22e4cc3f65778a60b359842bcec82'
Thanks in advance.
You can use the implode as the above answers suggested. If you directly want to modify the array then you could make use of array_walk().
<?php
$arr=Array
(
0 => '92ecd33db4ddcdc28e025cae80f00208',
1 => '9def02e6337b888d6dbe5617a172c18d',
2 => 'a6d22e4cc3f65778a60b359842bcec82',
);
array_walk($arr,function (&$val){ $val="'".$val."'";});
echo implode(',',$arr); //<----- This is the one you wanted by the way...
print_r($arr);
OUTPUT :
'92ecd33db4ddcdc28e025cae80f00208','9def02e6337b888d6dbe5617a172c18d','a6d22e4cc3f65778a60b359842bcec82'
Array
(
[0] => '92ecd33db4ddcdc28e025cae80f00208'
[1] => '9def02e6337b888d6dbe5617a172c18d'
[2] => 'a6d22e4cc3f65778a60b359842bcec82'
)
$user_id_arr = array
(
'0' => '92ecd33db4ddcdc28e025cae80f00208',
'1' => '9def02e6337b888d6dbe5617a172c18d',
'2' => 'a6d22e4cc3f65778a60b359842bcec82'
);
$user_ids = "'". implode("','",$user_id_arr)."'";
echo $user_ids;
Try
$user_ids = "'".implode("','", $arr)."'";
See demo
foreach($user_id_arr as $r){
$user_ids = $user_ids."'".$r."',";
}

Categories