Passing array into in_array()
up vote
0
down vote
favorite
Please consider the following PHP:
function get_ship_class()
{
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
foreach ($csv as $row)
{
$array = $row[$col];
}
}
How do I pass the resulting array from the above function into
if( in_array() ){
//code
}
?
php arrays
|
show 1 more comment
up vote
0
down vote
favorite
Please consider the following PHP:
function get_ship_class()
{
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
foreach ($csv as $row)
{
$array = $row[$col];
}
}
How do I pass the resulting array from the above function into
if( in_array() ){
//code
}
?
php arrays
1
You have to return it, then pass the function call into it
– Qirel
Nov 22 at 14:06
in function you can use return $array; and the when you call get_ship_class() function you can retrieve your array
– Sfili_81
Nov 22 at 14:08
where did $array come from ?
– Andrew
Nov 22 at 14:10
@Andrew the function was borrowed from this answer. Is it incorrect?
– ptrcao
Nov 22 at 14:12
3
@ptrcao I'm on mobile right now, seems like you've got a solid answer below :-)
– Qirel
Nov 22 at 14:26
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Please consider the following PHP:
function get_ship_class()
{
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
foreach ($csv as $row)
{
$array = $row[$col];
}
}
How do I pass the resulting array from the above function into
if( in_array() ){
//code
}
?
php arrays
Please consider the following PHP:
function get_ship_class()
{
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
foreach ($csv as $row)
{
$array = $row[$col];
}
}
How do I pass the resulting array from the above function into
if( in_array() ){
//code
}
?
php arrays
php arrays
edited Nov 22 at 14:11
treyBake
2,8063831
2,8063831
asked Nov 22 at 14:04
ptrcao
23911
23911
1
You have to return it, then pass the function call into it
– Qirel
Nov 22 at 14:06
in function you can use return $array; and the when you call get_ship_class() function you can retrieve your array
– Sfili_81
Nov 22 at 14:08
where did $array come from ?
– Andrew
Nov 22 at 14:10
@Andrew the function was borrowed from this answer. Is it incorrect?
– ptrcao
Nov 22 at 14:12
3
@ptrcao I'm on mobile right now, seems like you've got a solid answer below :-)
– Qirel
Nov 22 at 14:26
|
show 1 more comment
1
You have to return it, then pass the function call into it
– Qirel
Nov 22 at 14:06
in function you can use return $array; and the when you call get_ship_class() function you can retrieve your array
– Sfili_81
Nov 22 at 14:08
where did $array come from ?
– Andrew
Nov 22 at 14:10
@Andrew the function was borrowed from this answer. Is it incorrect?
– ptrcao
Nov 22 at 14:12
3
@ptrcao I'm on mobile right now, seems like you've got a solid answer below :-)
– Qirel
Nov 22 at 14:26
1
1
You have to return it, then pass the function call into it
– Qirel
Nov 22 at 14:06
You have to return it, then pass the function call into it
– Qirel
Nov 22 at 14:06
in function you can use return $array; and the when you call get_ship_class() function you can retrieve your array
– Sfili_81
Nov 22 at 14:08
in function you can use return $array; and the when you call get_ship_class() function you can retrieve your array
– Sfili_81
Nov 22 at 14:08
where did $array come from ?
– Andrew
Nov 22 at 14:10
where did $array come from ?
– Andrew
Nov 22 at 14:10
@Andrew the function was borrowed from this answer. Is it incorrect?
– ptrcao
Nov 22 at 14:12
@Andrew the function was borrowed from this answer. Is it incorrect?
– ptrcao
Nov 22 at 14:12
3
3
@ptrcao I'm on mobile right now, seems like you've got a solid answer below :-)
– Qirel
Nov 22 at 14:26
@ptrcao I'm on mobile right now, seems like you've got a solid answer below :-)
– Qirel
Nov 22 at 14:26
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
7
down vote
A slightly abbreviated version, but the same as is being suggested is to return the required data from the function but using array_column()
to extract the data...
function get_ship_class()
{
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
// Pass the extracted column back to calling method
return array_column($csv,$col);
}
And to use it...
if ( in_array( "somevalue", get_ship_class() )) {
//Process
}
If you are going to use this returned value a few times, it may be worth storing it in a variable rather than passing it straight into the in_array()
method.
add a comment |
up vote
1
down vote
This is the answer that the comments are suggesting.
function get_ship_class(){
$array = array();
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
foreach ($csv as $row) {
array_push($array, $row[$col]);
// array_push($array, "$row[$col]"); // You may need it as a string instead.
}
return $array;
}
if( in_array("whatever_you_are_looking_for", get_ship_class()) ){
//code
}
2
in_array need 2 arguments or i wrong?
– Sfili_81
Nov 22 at 14:15
2
@Sfili_81 You are absolutely correct. Amended.
– cmprogram
Nov 22 at 14:17
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
A slightly abbreviated version, but the same as is being suggested is to return the required data from the function but using array_column()
to extract the data...
function get_ship_class()
{
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
// Pass the extracted column back to calling method
return array_column($csv,$col);
}
And to use it...
if ( in_array( "somevalue", get_ship_class() )) {
//Process
}
If you are going to use this returned value a few times, it may be worth storing it in a variable rather than passing it straight into the in_array()
method.
add a comment |
up vote
7
down vote
A slightly abbreviated version, but the same as is being suggested is to return the required data from the function but using array_column()
to extract the data...
function get_ship_class()
{
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
// Pass the extracted column back to calling method
return array_column($csv,$col);
}
And to use it...
if ( in_array( "somevalue", get_ship_class() )) {
//Process
}
If you are going to use this returned value a few times, it may be worth storing it in a variable rather than passing it straight into the in_array()
method.
add a comment |
up vote
7
down vote
up vote
7
down vote
A slightly abbreviated version, but the same as is being suggested is to return the required data from the function but using array_column()
to extract the data...
function get_ship_class()
{
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
// Pass the extracted column back to calling method
return array_column($csv,$col);
}
And to use it...
if ( in_array( "somevalue", get_ship_class() )) {
//Process
}
If you are going to use this returned value a few times, it may be worth storing it in a variable rather than passing it straight into the in_array()
method.
A slightly abbreviated version, but the same as is being suggested is to return the required data from the function but using array_column()
to extract the data...
function get_ship_class()
{
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
// Pass the extracted column back to calling method
return array_column($csv,$col);
}
And to use it...
if ( in_array( "somevalue", get_ship_class() )) {
//Process
}
If you are going to use this returned value a few times, it may be worth storing it in a variable rather than passing it straight into the in_array()
method.
answered Nov 22 at 14:14
Nigel Ren
23.6k61832
23.6k61832
add a comment |
add a comment |
up vote
1
down vote
This is the answer that the comments are suggesting.
function get_ship_class(){
$array = array();
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
foreach ($csv as $row) {
array_push($array, $row[$col]);
// array_push($array, "$row[$col]"); // You may need it as a string instead.
}
return $array;
}
if( in_array("whatever_you_are_looking_for", get_ship_class()) ){
//code
}
2
in_array need 2 arguments or i wrong?
– Sfili_81
Nov 22 at 14:15
2
@Sfili_81 You are absolutely correct. Amended.
– cmprogram
Nov 22 at 14:17
add a comment |
up vote
1
down vote
This is the answer that the comments are suggesting.
function get_ship_class(){
$array = array();
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
foreach ($csv as $row) {
array_push($array, $row[$col]);
// array_push($array, "$row[$col]"); // You may need it as a string instead.
}
return $array;
}
if( in_array("whatever_you_are_looking_for", get_ship_class()) ){
//code
}
2
in_array need 2 arguments or i wrong?
– Sfili_81
Nov 22 at 14:15
2
@Sfili_81 You are absolutely correct. Amended.
– cmprogram
Nov 22 at 14:17
add a comment |
up vote
1
down vote
up vote
1
down vote
This is the answer that the comments are suggesting.
function get_ship_class(){
$array = array();
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
foreach ($csv as $row) {
array_push($array, $row[$col]);
// array_push($array, "$row[$col]"); // You may need it as a string instead.
}
return $array;
}
if( in_array("whatever_you_are_looking_for", get_ship_class()) ){
//code
}
This is the answer that the comments are suggesting.
function get_ship_class(){
$array = array();
$csv = array_map("str_getcsv", file("somefile.csv", "r"));
$header = array_shift($csv);
// Seperate the header from data
$col = array_search("heavy_shipping_class", $header);
foreach ($csv as $row) {
array_push($array, $row[$col]);
// array_push($array, "$row[$col]"); // You may need it as a string instead.
}
return $array;
}
if( in_array("whatever_you_are_looking_for", get_ship_class()) ){
//code
}
edited Nov 22 at 14:16
answered Nov 22 at 14:13
cmprogram
1,063519
1,063519
2
in_array need 2 arguments or i wrong?
– Sfili_81
Nov 22 at 14:15
2
@Sfili_81 You are absolutely correct. Amended.
– cmprogram
Nov 22 at 14:17
add a comment |
2
in_array need 2 arguments or i wrong?
– Sfili_81
Nov 22 at 14:15
2
@Sfili_81 You are absolutely correct. Amended.
– cmprogram
Nov 22 at 14:17
2
2
in_array need 2 arguments or i wrong?
– Sfili_81
Nov 22 at 14:15
in_array need 2 arguments or i wrong?
– Sfili_81
Nov 22 at 14:15
2
2
@Sfili_81 You are absolutely correct. Amended.
– cmprogram
Nov 22 at 14:17
@Sfili_81 You are absolutely correct. Amended.
– cmprogram
Nov 22 at 14:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432709%2fpassing-array-into-in-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You have to return it, then pass the function call into it
– Qirel
Nov 22 at 14:06
in function you can use return $array; and the when you call get_ship_class() function you can retrieve your array
– Sfili_81
Nov 22 at 14:08
where did $array come from ?
– Andrew
Nov 22 at 14:10
@Andrew the function was borrowed from this answer. Is it incorrect?
– ptrcao
Nov 22 at 14:12
3
@ptrcao I'm on mobile right now, seems like you've got a solid answer below :-)
– Qirel
Nov 22 at 14:26