Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hello to All i get a result from a server like this.
Id=6528537&CCode=250&Fild1=&Fild2=&Fild3=&HeshASM=0
How Can i convert it into a arary in php.
Thanks for the Help.
$query = 'Id=6528537&CCode=250&Fild1=&Fild2=&Fild3=&HeshASM=0';
parse_str($query, $output);
echo '<pre>';
print_r($output);
echo '</pre>';
/*
Array
(
[Id] => 6528537
[CCode] => 250
[Fild1] =>
[Fild2] =>
[Fild3] =>
[HeshASM] => 0
)
*/
You could try to construct the array yourself:
$string = "Id=6528537&CCode=250&Fild1=&Fild2=&Fild3=&HeshASM=0";
$explode = explode("&", $string);
$array = [];
foreach ($array as $key => $value) {
$inner_explode = explode("=", $value);
$array[$inner_explode[0]] = $inner_explode[1];
}
You would explode the string using "&" as separator, that will give you an array with the different parameters like array('id=6528537','ccode=250'...)
Then you will iterate trough that array, and explode again, this time using "=" as separator, this will give you the different parts of the different parameters, like: array('Id','6528537').
Knowing that in the position 0 of that array you will have the key, and in the position 1 the value, you simply add to your array the values in each iteration with $array[$inner_explode[0]] = $inner_explode[1];
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array that looks like this:
Array ( [0] => 1 [1] => 1 [2] => 3 )
I'd like to get the sum of the values excluding the last value.
So in this example I'm looking to find the answer to 1 + 1.
The array could be varying lengths.
You can do like this
$myArray= [1,1,3];
array_pop($myArray); // Remove the last element from array
echo array_sum($myArray); // Sum the values of the array
You can do something like this
<?php $array = array(1, 1, 3);
$sliced = array_slice($array, 0, -1); ?>
This will return you 1 & 1 it won't give you 3
<?php $value = array(1,1,3);
$removed = array_pop($value);
echo array_sum($value);
?>
Another way to do using closure
$array = [1,1,3];
echo array_sum(array_filter($array, function($key) use ($array) {
return count($array) - 1 !== $key;
}, ARRAY_FILTER_USE_KEY));
try this its very simple & fastest way
// initialize array by 'short syntax'
$a = [
1,1,3
];
//remove last element of array no matter length
// This function will reset() the array pointer of the input array after use.
array_pop($a);
// then you can sum of rest array values by array_sum
echo array_sum($a);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have an array
Array(
[0] => Column 1, Column 2 ,Column 3,Column 4,Column 5,Column 6
[1] => 19506,7254,321878,8,1,60
[2] => 187486,685,377,3,0,0
[3] => 187498,682285,3354,45,1,400
[4] => 18498,681285,38959,2,0,0
)
store into a mysql table using a loop or any other solutions
how to insert in database
I have exact same columns in a table
Use below snippet to convert it into array to save into database,
// replace spaces with "" and explode with comma
$keys = explode(",", str_replace(" ","",$arr[0]));
array_shift($arr); // remove first index
$arr = array_map(function($a) use($keys){
// first value as keys and rest array values exploding and combining as key value pair
return array_combine($keys, explode(",",$a));
}, $arr);
Demo
Code to create an insert query
$sql = "INSERT INTO table_name (".implode(",",$keys).") VALUES ";
foreach($arr as $data){
$sql .= "(".implode(",",$data)."),";
}
$sql = rtrim($sql,",");
echo $sql;
Output
INSERT INTO table_name (Column1,Column2,Column3,Column4,Column5,Column6)
VALUES (19506,7254,321878,8,1,60),(187486,685,377,3,0,0),
(187498,682285,3354,45,1,400),(18498,681285,38959,2,0,0)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my array I have these values:
[10d, 10e] => 4
The values are retrieved from another website and the important values will be put in this array.
Now I want to have two single values from these like:
[10d] => 2 [10e] => 2
How can I do this?
Now I'm guessing a little because of the description that is not that clear.
$arr = array('10d, 10e' => 4);
$newArr = array();
foreach($arr as $key => $value) {
$newKeys = explode(',', $key);
foreach($newKeys as $item) {
$newArr[trim($item)] = $value / count($newKeys);
}
}
print_r($newArr);
Result
[10d] => 2 [10e] => 2
Some functions you can use:
foreach() to loop the input array
explode() to split the key
trim() to remove whitespace
count() to, well, count array items
And, of course, / to divide ;-)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have associate array like this :
$json_data = array();
$jason_data[]= array ('id'=>'1','brand'=>'chanel','name'=>'red');
$jason_data[]= array ('id'=>'3','brand'=>'lacoste','name'=>'green');
$jason_data[]= array ('id'=>'1','brand'=>'chanel','name'=>'red');
$jason_data[0] and $jason_data[2] are Equal
i want find in $jason_data for equal array and echo them
I don't know whats the context of this and your question is vague, but anyway, I hope this is what you want to achieve. (and your variable naming is odd). In your example index zero and two are the same, and since they are multi-dimensional, you can flatten them in a way by using serialize. Try this:
$json_data = array();
$json_data[]= array ('id'=>'1','brand'=>'chanel','name'=>'red');
$json_data[]= array ('id'=>'3','brand'=>'lacoste','name'=>'green');
$json_data[]= array ('id'=>'1','brand'=>'chanel','name'=>'red');
$json_data = array_map('serialize', $json_data);
$values = array_count_values($json_data);
echo '<pre>';
foreach($values as $array => $count) {
if($count > 1) {
$array = unserialize($array);
print_r($array);
}
}
Should output/print the ones that has duplicates:
Array
(
[id] => 1
[brand] => chanel
[name] => red
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i want show array without []=> mycod is
foreach ($results as $value=>$value2){ //for array $ resullt is array
$array2=print_r($value2);
return true;
}
Array ( [0] => test [1] => test [2] => test [3] => test [4] => test)
Try this:
echo implode("", $array);
Don't use print_r then, or var_dump for that matter. Just set $array2 = $value2;
To only echo the values you can do this:
foreach ($results as $key=>$value){ //for array $ resullt is array
echo $value . "<br />";
}
you can not have array without keys.
you can access array elements by looping through it and can create formatted output. like this:
$strOutput = "";
foreach ($results as $value=>$value2){
$strOutput .= $value2 ." ";
}
echo $strOutput;