How to delete last element in a 2d array? - php

I am getting an array in through form .I am using explode method to separate my values
$getData =$_POST['jasonHandle'];
print_r($getData); // output incoetp,11,12,13,#,101,11,12,#
here # is used for separation
$arr=explode(",#,",$getData);
print_r($arr); // Array ( [0] => incoetp,11,12,13 [1] => 101,11,12,# )
The above array may contain any number of elements(in this case there are tow arrays) .
I need to delete this # element which is appearing at last of last array
I tried unset() and array_pop () ,but its deleting entire last array .
Also COUNT() function is not working for inner array
echo count($arr[1]); // Parameter must be an array or an object that implements Countable
I have been using count function for 2 d array and never had any issue .why is this happening?

you can use chop() function
chop()--> The chop() function removes whitespaces or other predefined characters from the right end of a string
$arr[1]=chop($arr[1],",#");

Delete it by using rtrim
$arr[1] = rtrim($arr[1], '#');

Related

How to get the first unique number in array using PHP?

Consider an array [1,2,1,2,3,4,5,]. I need to make a function in PHP that will return the first unique number in that array, in this case, 3.
Or
How we can remove all repeated elements of the array. In this case returns [3,4,5].
You can use following snippet,
$yourArr = [1,2,1,2,3,4,5];
// count the number of occurences of each value
$res = array_count_values($yourArr);
// filtering only unique values
$res = array_filter($res, function($item){ return $item == 1; });
print_r();
// to fetched first unique
// fetching filtered values as keys
$un = array_keys($res);
echo $un[0]; // will output 3
array_count_values — Counts all the values of an array
array_filter — Filters elements of an array using a callback function
array_keys — Return all the keys or a subset of the keys of an array
Working Demo
Output:-
Array
(
[0] => 3
[1] => 4
[2] => 5
)

How to get part of the string from an array element, and insert it into another array?

I have an array which returns something like this:
[44] => 165,text:Where is this city:,photo:c157,correct:0,answers:[{text:Pery.,correct:true},{text:Cuba.,correct:false},{text:Brazil.,correct:false}]},{
I would like to get all the numbers from the beginning of the string until the first occurrence of a comma in the array element value. In this case that would be number 165 and I want to place that number in another array named $newQuesitons as key questionID
Next part will be to get the string after the first occurrence of : until the next occurrence of : and add it into the same array ($newQuestions) as key question.
Next part will be the photo:, that is I need to get the string after the photo: until the next occurrence of the comma, in this case peace of the string extracted will be c157.
I would like to add that as new key named photo in the array $newQuestions
I think this may be able to help you
<?php
$input = '165,text:Where is this city:,photo:c157,correct:0';
//define our new array
$newQuestions = Array();
//first part states we need to get all the numbers from the beginning of the string until the first occurence of a ',' as this is our array key
//$arrayKey[0] is our arrayKey
$arrayKey = explode(',',$input);
//second part requires us to loop through the array and split up the strings by comma and colon
foreach($arrayKey as $data){
//split each text into 2 by the colon
$item = explode(':',$data);
//we are only interested in items that have a colon in them, if we split it and the input has no colon, the count would be 0, so this check is used to ignore those
if(count($item) > 0) {
//now we can build our array
$newQuestions[$arrayKey[0]][$item[0]] = $item[1];
}
}
//output array
print_r($newQuestions);
?>
I don't fully understand the inputted array so the code above will most likely have to be tweaked, but atleast it gives you some logic to go from.
The output of this was: Array ( [165] => Array ( [165] => [text] => Where is this city [photo] => c157 [correct] => 0 ) )
I get my own solution, at least for the part of the problem. I manage to get the questionID using the following code:
$newQuestions = array();
foreach ($arrQuestions as $key => $question) {
$substring = substr($question, 0, strpos($question, ','));
$newQuestions[]['questionID'] = $substring;
}
I am now trying to do the same thing for the question part. I will update this code in case that someone else may have similar task to accomplish.

Using array_diff(), how to get difference from array2 instead of array1?

I'm trying to use array_diff like so. Here are my two array outputs:
List 1 Output
Array ([0] => 0022806 )
List 2 Output
Array ([0] => 0022806 [1] => 0023199 )
PHP
$diff = array_diff($list_1, $list_2);
print "DIFF: " . count($diff) ."<br>";
print_r($diff);
The Output is:
DIFF: 0
Array ( )
Any idea what I'm doing wrong? Why is 0023199 not returned?
The order of arguments in array_diff() is important
Returns an array containing all the entries from array1 that are not
present in any of the other arrays
try;
$diff = array_merge(array_diff($list_1, $list_2), array_diff($list_2, $list_1));
print "DIFF: " . count($diff) ."<br>";
print_r($diff);
From the docs:
Returns an array containing all the entries from array1 that are not
present in any of the other arrays.
If you only want to check whether they are the same, you can use $list1 == $list_2
Per the documentation, the values of the second array are subtracted from the first one. Or, to put it another way, you start with the first array, and then remove all the values that appear in the second array. That would correctly yield an empty array that you see above
You might want to play around with intersection, that might help you get what you want.

Error in hierarchical php function exectuion! What's this?

Why am I getting different outputs through print_r in the following two cases!!? This is a bug in php? Is php unable to execute complex hierarchical functions called inside functions?
CASE 1 :
$aa='2,3,4,5,5,5,';
$aa=array_unique(explode(',',$aa));
array_pop($aa);
print_r($aa);
CASE 2 :
$aa='2,3,4,5,5,5,';
array_pop(array_unique(explode(',',$aa)));
print_r($aa)
In the first case, the output is an exploded array :
Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 )
In the second case, the output is string :
2,3,4,5,5,5,
This is because array_pop alters its input, and you're passing it a temporary variable (not $aa).
Note the signature in the documentation: array_pop ( array &$array ) - the & means it takes a parameter by reference, and it alters that input variable.
Compare with the other two functions:
array explode ( string $delimiter , string $string , int $limit )
and
array array_unique ( array $array , int $sort_flags = SORT_STRING )
In the first case you update $aa with the output of array_unique(), and then pass that to array_pop to be altered.
In the second case the output of array_unique() will be the same, but this temporary value isn't assigned to a variable & therefore it's forgotten after array_pop is called.
It's worth noting that in that in PHP (unlike say, C++), passing by reference is actually slower than passing by value and therefore is only ever used to modify the input parameter of a function.
In the first case you alter variable as in line 2 bs assigning a new value with the assignment operator =

An array is ending with a character(")") inside array element value, not with actual array end

An array which is populated it's value from database. when I print_r the array . It shows like below...
Array
(
[term] => These are following selection a) new b) old
)
Here the array is ending with character ) inside the term element " ..selection a) new ..", while it's only a character in the array. So How I would avoid the ending of array with charcters present inside the array ?
What you're viewing is the output of an array with print_r.
Defining an array in PHP is a little different:
$variable = array('inside First a) new, b) old');
print_r($variable);
var_dump($variable); // similar to print_r
There is also another way of doing that:
$variable = array();
$variable[0] = 'inside First a) new, b) old';
So you need to quote your value to get a string.
The array value in your declaration should be a string:
$myArray = array(
0 => 'inside First a) new , b) old';
);
In PHP you need to quote string values:
$array = array(
0 => 'inside First a) new , b) old'
);
See the manual. To be honest this is pretty basic stuff. If you don't know this, I'd suggest going away and getting a basic introduction to PHP book and reading through it...

Categories