Insert specific value on first $key in array - php

This is really grinding my gears:
if(isset($_POST['does_what'])){
$strings = array();
foreach($_POST['does_what'] as $key => $value){
if($value[$key] == 0){
$strings[0] = "This is $value";
}
$strings[] = $value;
}
}
And this gives me error: PHP Notice: Uninitialized string offset: 0
Im trying to insert "some extra" text on first key of an array. And the other keys should your be inserted.

Use array_unshift
if(!empty($_POST['does_what']) && is_array($_POST['does_what'])){
$strings = array();
foreach($_POST['does_what'] as $key => $value){
if($value[$key] == 0){
$text = "This is ".$value
$value = array_unshift($strings[$key], $text );
}
else{
echo "Value is not a Zero";
}
$strings[] = $value;
}
}
else{
echo "Post is empty Or its not an array";
}
array_unshift Example

I think you want something more like this:
foreach($_POST as $key => $value){
if (count( $strings) == 0)
$strings[] = "This is $value";
else
$strings[] = $value;

Related

How do I filter php json for matching keys

I intend the php function to return me the list of all the keys if the value equal or are less than the variable
<?php
$t = 35;
$jsonobj = '{"ABC":35,"DEF":36,"GEH":34}';
$obj = json_decode($jsonobj);
foreach($obj as $key => $value){
if ($t <= $value) {
{echo $key . " => " . $value . "<br>";}}
else{
echo "No result match";
}
}
?>
Decode your JSON as array an use array_filter() to filter out elements:
$jsonobj = '{"ABC":35,"DEF":36,"GEH":34}';
$array = json_decode($jsonobj, true);
print_r(array_filter($array, "lessthan"));
function lessthan($var) {
return $var <= 35;
}
Will output
Array
(
[ABC] => 35
[GEH] => 34
)
Edit: if you want to use/keep your $t you can do like so:
<?php
$t = 35;
$jsonobj = '{"ABC":35,"DEF":36,"GEH":34}';
$array = json_decode($jsonobj, true);
print_r(array_filter($array, function ($var) use ($t) {
return $var <= $t;
}));
If you want to maintain your style of coding then I recommend looping the array and adding to a new array if true.
$t = 35;
$jsonobj = '{"ABC":35,"DEF":36,"GEH":34}';
$obj = json_decode($jsonobj);
$match =[];
foreach($obj as $key => $value){
if ($t <= $value) {
$match[] = $key . " => " . $value;
}
}
if(count($match) > 0){
echo implode("<br>\n", $match);
}else{
echo "no matches";
}
See it running here:
https://3v4l.org/NDuD7

How to limit array in array

I want to limit every array to strings with 15 characters or less. I have tried this code, but it does not work:
$a = [
"name1" => ['Dewa','Aditya','Pratama'],
"name2" => ['Brian','Dzikri','Ramadhan'],
];
$result_shortdes = "";
foreach ($a as $values) {
foreach ($values as $value) {
if(strlen($result_shortdes) + strlen($value) <= 15)
{
$result_shortdes .= "$value,";
}
}
}
echo '<pre>';
print_r($result_shortdes);
echo '<pre>';
My expected output is like this:
1. Dewa,Aditya,
2. Brian,
Every time you go to the next name, you need to reset result_shortdes to count the name length again, place the variable inside the first loop like this:
foreach ($a as $values) {
$result_shortdes = "";
foreach ($values as $value) {
if(strlen($result_shortdes) + strlen($value) <= 15)
{
$result_shortdes .= "$value,";
}
}
echo '<pre>';
print_r($result_shortdes);
echo '<pre>';
}
you can use $result_shortdes for length and $result to store result like below
$a = [
"name1" => ['Dewa','Aditya','Pratama'],
"name2" => ['Brian','Dzikri','Ramadhan'],
];
$result_shortdes = "";
$result = [];
foreach ($a as $values) {
$result_shortdes = "";
foreach ($values as $value) {
if(strlen($result_shortdes) + strlen($value) <= 15)
{
$result_shortdes .= "$value,";
}else{
$result[] = $result_shortdes;
break;
}
}
}
echo '<pre>';
print_r($result);
echo '<pre>';

PHP How to merge array element with next while maintaining order?

$array = ['coke.','fanta.','chocolate.'];
foreach ($array as $key => $value) {
if (strlen($value)<6) {
$new[] = $value." ".$array[$key+1];
} else {
$new[] = $value;
}
}
This code doesn't have the desired effect, in fact it doesn't work at all. What I want to do is if an array element has string length less than 5, join it with the next element. So in this case the array should turn into this:
$array = ['coke. fanta.','chocolate.'];
$array = ['coke.','fanta.','chocolate.', 'candy'];
$new = [];
reset($array); // ensure internal pointer is at start
do{
$val = current($array); // capture current value
if(strlen($val)>=6):
$new[] = $val; // long string; add to $new
// short string. Concatenate with next value
// (note this moves array pointer forward)
else:
$nextVal = next($array) ? : '';
$new[] = trim($val . ' ' . $nextVal);
endif;
}while(next($array));
print_r($new); // what you want
Live demo
With array_reduce:
$array = ['coke.', 'fanta.', 'chocolate.', 'a.', 'b.', 'c.', 'd.'];
$result = array_reduce($array, function($c, $i) {
if ( strlen(end($c)) < 6 )
$c[key($c)] .= empty(current($c)) ? $i : " $i";
else
$c[] = $i;
return $c;
}, ['']);
print_r($result);
demo
<pre>
$array = ['coke.','fanta.','chocolate.'];
print_r($array);
echo "<pre>";
$next_merge = "";
foreach ($array as $key => $value) {
if($next_merge == $value){
continue;
}
if (strlen($value)<6) {
$new[] = $value." ".$array[$key+1];
$next_merge = $array[$key+1];
} else {
$new[] = $value;
}
}
print_r($new);
</pre>
Updated Code after adding pop after chocolate.
<pre>
$array = ['coke.','fanta.','chocolate.','pop'];
print_r($array);
echo "<br>";
$next_merge = "";
foreach ($array as $key => $value) {
if($next_merge == $value){
continue;
}
if (strlen($value)<6 && !empty($array[$key+1])) {
$new[] = $value." ".$array[$key+1];
$next_merge = $array[$key+1];
} else {
$new[] = $value;
}
}
print_r($new);
<pre>
You need to skip the iteration for the values that you have already added.
$array = ['coke.', 'fanta.', 'chocolate.'];
$cont = false;
foreach ($array as $key => $value) {
if ($cont) {
$cont = false;
continue;
}
if (strlen($value) < 6 && isset($array[$key+1])) {
$new[] = $value.' '.$array[$key+1];
$cont = true;
}
else {
$new[] = $value;
}
}
print_r($new);

Loop into an array with PHP

I have the following array:
$array = [
['2017-02-26', '2017-02-27'],
['2017-03-01'],
['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'],
['2017-01-05', '2017-01-06', '2017-01-07']
];
I'm looking to loop into this array to have something like this:
// When several dates
From 2017-02-26 to 2017-02-27.
// When only one date
On the 2017-03-01.
What I tried:
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
echo "On the $key[$value]";
}
else {
$first = reset($array);
$last = end($array);
echo "From ".$first." to ".$last.;
}
}
But it doesn't work when there is only one date in the row.
You are looping by foreach() so it will display last echo string .Store result to one variable Eg($display) will be more easy to display that
$display = "";
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
$display .= "On the $value[0] <br>";
}
else {
$first = $value[0];
$last = $value[$count-1];
$display .= "From ".$first." to ".$last."<br>";
}
}
echo $display;
Try this:-
foreach ($array as $key => $value) {
$count = count($value);
if($count==1) {
echo "On the ".$value[0];
}
else {
$first = reset($value);
$last = end($value);
echo "From ".$first." to ".$last;
}
}
Or just copy paste this code, it will work. Your main inside array to play with is $value.

how to count strings in php?

i have this situation"
foreach($front->getRequest()->getParams() as $key => $value){
if ($value == '1'){
$$key = $value;
}
}
echo $test1; // test1 = 1
echo $test2; // test2 = 1
....
this will give me back one or more $test = 1 where the $$key = $test and $value = 1
i want to see how many actually come back. and i was thinking to do something like: print_r(count($key)) or print_r(count($value)) but it doesn't tell me how many results are there
any ideas?
thanks
Why not just keep a counter?
$count = 0;
foreach($front->getRequest()->getParams() as $key => $value){
if ($value == '1'){
$$key = $value;
$count++;
}
}
echo $count;

Categories